package com.cjh.wechat.websocket.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cjh.wechat.commom.pojo.ChatRecord;
import com.cjh.wechat.commom.pojo.User;
import com.cjh.wechat.commom.util.ObjectSer;
import com.cjh.wechat.commom.vo.ChatRecordVo;
import com.cjh.wechat.websocket.service.ChatRecordService;
import com.cjh.wechat.websocket.service.RedisService;
import com.cjh.wechat.websocket.service.UserService;
import redis.clients.jedis.Jedis;

@Service
public class ChatRecordServiceImpl implements ChatRecordService
{
    private static final String FRIEND_RECORD = "friend";
    private static final String GROUP_RECORD  = "group";
    @Resource
    private RedisService        redisService;
    @Resource
    private UserService         userService;
    
    @Override
    public List<ChatRecordVo> getFriendRecord(String myId, String friendId, int pageNum, int pageSize)
    {
        try (Jedis jedis = redisService.get())
        {
            int startNum = (pageNum - 1) * pageSize;
            int endNum = (pageNum - 1) * pageSize + pageSize-1;
            Set<byte[]> set = jedis.zrange((FRIEND_RECORD + "_" + myId + friendId).getBytes(), startNum, endNum);
            Iterator<byte[]> iter = set.iterator();
            List<ChatRecordVo> list = new ArrayList<ChatRecordVo>();
            while (iter.hasNext())
            {
                ChatRecord chatRecord = (ChatRecord) ObjectSer.ByteToObject(iter.next());
                User user = userService.findById(chatRecord.getId());
                ChatRecordVo chatRecordVo = buildChatRecordVo(user, chatRecord);
                list.add(chatRecordVo);
            }
            return list;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return new ArrayList<ChatRecordVo>();
    }
    
    @Override
    public List<ChatRecordVo> getGroupRecord(String groupId, int pageNum, int pageSize)
    {
        try (Jedis jedis = redisService.get())
        {
            int startNum = (pageNum - 1) * pageSize;
            int endNum = (pageNum - 1) * pageSize + pageSize-1;
            Set<byte[]> set = jedis.zrange((GROUP_RECORD + "_" + groupId).getBytes(), startNum, endNum);
            Iterator<byte[]> iter = set.iterator();
            List<ChatRecordVo> list = new ArrayList<ChatRecordVo>();
            while (iter.hasNext())
            {
                ChatRecord chatRecord = (ChatRecord) ObjectSer.ByteToObject(iter.next());
                User user = userService.findById(chatRecord.getId());
                ChatRecordVo chatRecordVo = buildChatRecordVo(user, chatRecord);
                list.add(chatRecordVo);
            }        
            return list;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return new ArrayList<ChatRecordVo>();
    }
    
    @Override
    public int getGroupRecordNum(String groupId)
    {
        try (Jedis jedis = redisService.get())
        {
            Set<byte[]> set = jedis.zrevrange((GROUP_RECORD + "_" + groupId).getBytes(), 0, -1);
            return set.size();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }
    
    @Override
    public int getFriendRecordNum(String myId, String friendId)
    {
        try (Jedis jedis = redisService.get())
        {
            Set<byte[]> set = jedis.zrevrange((FRIEND_RECORD + "_" + myId + friendId).getBytes(), 0, -1);
            return set.size();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }
    
    @Override
    public void addFriendRecord(String myId, String friendId, ChatRecord chatRecord)
    {
        try (Jedis jedis = redisService.get())
        {
            jedis.zadd((FRIEND_RECORD + "_" + myId + friendId).getBytes(), //
                    Double.parseDouble(chatRecord.getDateTime()), ObjectSer.ObjectToByte(chatRecord));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    @Override
    public void addGroupRecord(String groupId, ChatRecord chatRecord)
    {
        try (Jedis jedis = redisService.get())
        {
            jedis.zadd((GROUP_RECORD + "_" + groupId).getBytes(), //
                    Double.parseDouble(chatRecord.getDateTime()), ObjectSer.ObjectToByte(chatRecord));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    private ChatRecordVo buildChatRecordVo(User user, ChatRecord chatRecord)
    {
        ChatRecordVo chatRecordVo = new ChatRecordVo();
        try
        {
            chatRecordVo.setAvatar(user.getAvatar());
            chatRecordVo.setUserName(user.getUsername());
            chatRecordVo.setContent(chatRecord.getContent());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            Date date = sdf.parse(chatRecord.getDateTime());
            chatRecordVo.setDateTime(sdf2.format(date));
            chatRecordVo.setId(chatRecord.getId());
            chatRecordVo.setType(chatRecord.getType());
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return chatRecordVo;
    }
}
